package com.zwh.develop;

/**
 * @author jxl
 * @date 2022/1/14
 */

/**
 *根据查询sql, 创建java对象
 *
 * @author jxl
 */
public class CreateJavaObjectBySql {

    public static void main(String[] args) {
        String sql = "select b.outpatrecipeid_chr, r.itemid_chr,  r.itemname_vchr,  d.itemcode_vchr,   d.itemspec_vchr,  c.unitid_chr,    r.price_dec,    r.orgamout_dec,   r.retamout_dec, 1 as medflag, c.discount_dec,                                   c.recipetype_int,  c.execdeptid_chr,                                  c.billrowno_int,                                   c.unittype_int,                                 c.unitscale_dec,                                   c.ispatientmed_int,                                   d.itemopcalctype_chr,                                   d.itemopinvtype_chr,                                   t.typename_vchr as invname,                                   c.toldiffprice_mny\n" +
			" from t_opr_charge a,  t_opr_reciperelation b,  t_opr_oprecipeitemde c,  t_bse_chargeitem d,\n" +
			" (select distinct t1.outpatrecipeid_chr, t2.itemid_chr,t2.itemname_vchr, t2.price_dec, t2.billrowno_int, t2.orgamout_dec,t2.retamout_dec \n" +
			" from t_opr_returnmed t1, t_opr_returnmed_entry t2  where t1.outpatrecipeid_chr = t2.outpatrecipeid_chr and t1.status_int = 1) r,\n" +
			"(select typeid_chr, typename_vchr from t_bse_chargeitemextype  where flag_int = 2) t\n" +
			"  where a.chargeno_chr = b.chargeno_chr\n" +
			"  and b.outpatrecipeid_chr = c.outpatrecipeid_chr and b.outpatrecipeid_chr = r.outpatrecipeid_chr and c.billrowno_int = r.billrowno_int    and (c.recipetype_int = '0001' or c.recipetype_int = '0002' or    c.recipetype_int = '0006')\n" +
			"  and r.itemid_chr = c.itemid_chr and r.itemid_chr = d.itemid_chr and d.itemopinvtype_chr = t.typeid_chr  and a.chargeno_chr = '21090808434108428511'\n" +
			"union all\n" +
			"select b.outpatrecipeid_chr,  c.itemid_chr,  d.itemname_vchr,  d.itemcode_vchr,  d.itemspec_vchr, c.unitid_chr,  c.price_mny as price_dec,  c.qty_dec as orgamout_dec, 0 as retamout_dec,  0 as medflag,   c.discount_dec,  c.recipetype_int,   c.execdeptid_chr,    c.billrowno_int,    c.unittype_int,  c.unitscale_dec,  c.ispatientmed_int, d.itemopcalctype_chr,    d.itemopinvtype_chr,  t.typename_vchr as invname,  c.toldiffprice_mny\n" +
			" from t_opr_charge a,  t_opr_reciperelation b, t_opr_oprecipeitemde c,   t_bse_chargeitem d,\n" +
			" (select typeid_chr, typename_vchr  from t_bse_chargeitemextype where flag_int = 2) t\n" +
			"where a.chargeno_chr = b.chargeno_chr and b.outpatrecipeid_chr = c.outpatrecipeid_chr  and c.itemid_chr = d.itemid_chr   and d.itemopinvtype_chr = t.typeid_chr   and (c.recipetype_int = '0001' or c.recipetype_int = '0002' or c.recipetype_int = '0006') \n" +
			"and a.chargeno_chr = '21090808434108428511' and not exists (select 1   from t_opr_returnmed_entry r  where b.outpatrecipeid_chr = r.outpatrecipeid_chr  and c.itemid_chr = r.itemid_chr   and c.billrowno_int = r.billrowno_int)";
        String tableName = getTableNameFromSql(sql);
        String[] propArr = getSqlProperties(sql);
        convert2JaveFields(propArr);
        String result = buildJavaObject(tableName, propArr);
        System.out.println(result);
    }

    private static String getTableNameFromSql(String sql) {
        int index = sql.indexOf("from");
        if (index < 0) {
            return "JavaObject";
        }
        String tableName = sql.substring(index + 4).trim().split(" ")[0];
        return tableName.toLowerCase().replaceAll("t_", "").trim();
    }

    private static String buildJavaObject(String tableName, String[] propArr) {
        tableName = convert2Filed(tableName.toLowerCase());
        String objName = (char) (tableName.charAt(0) - 32) + tableName.substring(1);

        StringBuffer sb = new StringBuffer();
        sb.append("@Data").append("\n");
        sb.append("public class ").append(objName).append("VO{").append("\n");
        for (String p : propArr) {
            sb.append(" private String ").append(p).append(";\n");
        }
        sb.append("}");
        return sb.toString();
    }

    private static void convert2JaveFields(String[] propArr) {

        int i = 0;
        for (String prop : propArr) {
            propArr[i++] = convert2Filed(prop);
        }
    }

    private static String convert2Filed(String prop) {
        prop = prop.trim();
        int index;
        StringBuffer sb = new StringBuffer();
        while ((index = prop.indexOf("_") )>=0){
            char c = prop.charAt(index + 1);
            c = (char) (c - 32);
            sb.append(prop,0, index).append(c);
            prop =  prop.substring(index + 2);
        }
        sb.append(prop);
        return sb.toString();
    }

    private static String[] getSqlProperties(String sql) {
        sql = sql.toLowerCase();
        int start = sql.indexOf("select");
        if(start<0){
            start = 0;
        }else{
            start+=6;
        }
        int end  = sql.indexOf("from");
        if(end<0){
            end = sql.length();
        }
        String subStr = sql.substring(start, end);
        String[] propArr = subStr.split(",");
        int length = propArr.length;
        for (int i = 0; i < length; i++) {
            String[] tempPropArr = propArr[i].trim().split(" ");
            propArr[i] = tempPropArr[tempPropArr.length-1];
            tempPropArr = propArr[i].trim().split("\\.");
            propArr[i] = tempPropArr[tempPropArr.length-1];
        }
        return propArr;
    }
}
